package net.yla.smartbus.module.controller.web.ba.right.admin;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.module.ba.right.admin.entity.AdminDO;
import net.yla.smartbus.module.ba.right.admin.query.*;
import net.yla.smartbus.module.ba.right.admin.service.AdminLoginLogsService;
import net.yla.smartbus.module.ba.right.admin.service.AdminService;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @Date: 2020-08-13 15:39
 * @Author: Being Nobody
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/web/admin")
public class AdminController extends BaseController {
    @Autowired
    private AdminService service;
    @Autowired
    private RolesService rolesService;

    /*====================================admin======================================*/

    /**
     * 1查询符合条件的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/count")
    public ApiResult count(@RequestBody AdminCount count) {
        return success(service.count(count));
    }

    /**
     * 2查询符合条件的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/query")
    public ApiResult query(@RequestBody AdminQuery query) {
        rolesService.userRoleEvaluation(UserRoleEnum.USER_ACCOUNT_SELECT,getToken(),getIpAddress());
        return success(service.admin(query));
    }

    /**
     * 3查看管理员记录
     * 主键ID
     *
     * @param query
     * @return
     */
    @PostMapping("/view")
    public ApiResult view(@RequestBody SaveQuery query) {
        return success(service.admin(query.getId()));
    }

    /**
     * 4创建或更新管理员账号
     *
     * @param query
     * @return
     */
    @PostMapping("/save")
    public ApiResult save(@RequestBody SaveQuery query) {
        rolesService.userRoleEvaluation(UserRoleEnum.USER_ACCOUNT_INSERT,getToken(),getIpAddress());
        int i = service.saveSelective(query);
        return success(i);
    }

    /**
     * 4解除管理员异常状态信息
     *
     * @param query
     * @return
     */
    @PostMapping("/reset")
    public ApiResult reset(@RequestBody SaveQuery query) {
        return success(query.getId());
    }

    /**
     * 5删除管理员账号信息
     *
     * @param query
     * @return
     */
    @PostMapping("/del")
    public ApiResult del(@RequestBody SaveQuery query) {
        rolesService.userRoleEvaluation(UserRoleEnum.USER_ACCOUNT_DELETE,getToken(),getIpAddress());
        return success(service.delAdmin(query.getId()));
    }
    /**
     * 批量删除管理员账号信息
     *
     * @param query
     * @return
     */
    @PostMapping("deleteByList")
    public ApiResult deleteByList(@RequestBody SaveQuery query){
        return success(service.deleteByList(query.getAdminIds()));
    }

    /**
     * 导出到Excel方法
     *
     * @param query
     * @return
     */
    @PostMapping("/export")
    public void export(AdminQuery query, HttpServletResponse response) {
        service.export(query, response);
    }
    /*====================================ip======================================*/

    /**
     * 6获取IP跟踪信息的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/ip/count")
    public ApiResult ipOfCount(@RequestBody IPCount count) {
        return success(service.ipOfCount(count));
    }

    /**
     * 7获取IP跟踪信息的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/ip/query")
    public ApiResult ipOfQuery( IPQuery query) {
        return success(service.ipOfQuery(query));
    }

    /**
     * 8查看IP跟踪信息记录
     *
     * @param traceId
     * @return
     */
    @PostMapping("/ip/view")
    public ApiResult ipOfView(@RequestBody Integer traceId) {
        return success(service.ipOfView(traceId));
    }

    /*====================================log======================================*/

    /**
     * 9获取管理员登录日志的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/log/count")
    public ApiResult logOfCount(@RequestBody LogCount count) {
        return success(service.logOfCount(count));
    }

    /**
     * 10查询管理员登录日志的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/log/query")
    public ApiResult logOfQuery(@RequestBody @Validated LogQuery query) {
        return success(service.logOfQuery(query));
    }

    /**
     * 11查询管理员登录日志记录
     *
     * @param query
     * @return
     */
    @PostMapping("/log/view")
    public ApiResult logOfView(@RequestBody SaveQuery query) {
        return success(service.logOfView(query.getId()));
    }
    /**
     * 12删除账户日志信息
     *
     * @param logId
     * @return
     */
    @PostMapping("/log/del")
    public ApiResult del(Integer logId){
        return success(service.deleteAdminLog(logId));
    }

    /**
     * 13批量删除账户日志信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/log/deleteByList")
    public ApiResult deleteByLogList(List<Integer> ids) {
        return success(service.deleteByLogList(ids));
    }

    /**
     * 14查询未绑定其他角色的全部账号
     *
     * @param id
     * @return
     */
    @PostMapping("/nobling/query")
    public ApiResult getAdminNoRoleInfo(Integer id) {
        return success(service.getAdminNoRoleInfo(id));
    }
}
